package com.dmap.run.service.impl;

import com.dmap.run.po.mongo.MapTrack;
import com.dmap.run.service.MapTrackService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * \* User: silent
 * \* Date: 16/05/18 Time: 18:34
 * \* Description:
 * \
 */
@Service
public class MapTrackServiceImpl implements MapTrackService {
    private static final Logger logger = LoggerFactory.getLogger(MapTrackServiceImpl.class);
    @Resource
    private MongoOperations mongoOperations;

    @Override
    public boolean insertMapTrack(MapTrack mapTrack) {
        try {
            mongoOperations.save(mapTrack);
        } catch (Exception e) {
            logger.error("mongodb insert common data error: {}", e.toString());
            return false;
        }
        return true;
    }

    @Override
    public MapTrack selectMapTrackByUserIdAndStartTimeAndEndTime(long userId, long startTime, long endTime) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        query.addCriteria(Criteria.where("startTime").is(startTime));
        query.addCriteria(Criteria.where("endTime").is(endTime));
        return mongoOperations.findOne(query, MapTrack.class);
    }
}
